package pers.linkong.asr.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import pers.linkong.asr.domain.TeacherBean;
import pers.linkong.asr.service.TeacherService;
import pers.linkong.asr.util.ResultInfo;

@Controller
public class LoginController {

	@Autowired
	private TeacherService teacherService;
	
	@RequestMapping("/login")
	@ResponseBody
	public ResultInfo login(int tid, String tpass, HttpSession session) {
		TeacherBean tBean = this.teacherService.login(tid, tpass);
		if(tBean == null){
			System.out.println("登录失败:" + tid + ":" + tpass);
			return ResultInfo.data(0, "登录失败，账号或者密码不正确");
		} else {
			System.out.println("登录成功:" + tid + ":" + tpass);
			session.setAttribute("loginTid", tBean.getTid());
			session.setAttribute("loginTname", tBean.getTname());
			return ResultInfo.data(1, tBean.getTname() + "欢迎您！");
		}
	}
	
	@RequestMapping("/loginpage")
	public String loginpage(){
		return "Login";
	}
	
	@RequestMapping("/logout")
	@ResponseBody
	public ResultInfo logout(HttpSession session) {
		session.setAttribute("loginTid", null);
		session.setAttribute("loginTname", null);
		return ResultInfo.data(1, "已登出，欢迎再次使用ASRemake版");
	}
}
